package edu.sjtu.erplab.cm.dao.impl;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import edu.sjtu.erplab.cm.dao.bean.CollaborativeModule;
import edu.sjtu.erplab.cm.util.CollaborativeModuleConstant;

@Repository("CollaborativeModuleDAO")
public class CollaborativeModuleDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ErrorCodeDAO.class);

	// DAO START

	public CollaborativeModule save(CollaborativeModule transientInstance) {
		log.debug("saving CollaborativeModule instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
			return transientInstance;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CollaborativeModule persistentInstance) {
		log.debug("deleting CollaborativeModule instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CollaborativeModule findById(java.lang.String id) {
		log.debug("finding CollaborativeModule instance by id: " + id);
		try {
			CollaborativeModule instance = (CollaborativeModule) getHibernateTemplate()
					.get("edu.sjtu.erplab.cm.dao.bean.CollaborativeModule", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("find CollaborativeModule instance by id failed", re);
			throw re;
		}
	}

	public List<CollaborativeModule> findByExample(CollaborativeModule instance) {
		log.debug("finding CollaborativeModule instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			return results;
		} catch (RuntimeException re) {
			log.error("find CollaborativeModule instance by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CollaborativeModule instance by property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CollaborativeModule as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find CollaborativeModule instance by property failed",
					re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all CollaborativeModule instances");
		try {
			String queryString = "from CollaborativeModule";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CollaborativeModule merge(CollaborativeModule detachedInstance) {
		log.debug("merging CollaborativeModule instance");
		try {
			CollaborativeModule result = (CollaborativeModule) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CollaborativeModule instance) {
		log.debug("attaching dirty CollaborativeModule instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CollaborativeModule instance) {
		log.debug("attaching clean CollaborativeModule instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	// DAO END

	// BUSINESS START

	// 没有找到返回空，数据库操作出错返回异常
	public CollaborativeModule findBy2Prams(String id, String updateType) {
		log.debug("finding CollaborativeModule instance by id and updateType: "
				+ id + "&" + updateType);
		try {
			CollaborativeModule cm = new CollaborativeModule();
			cm.setId(id);
			cm.setUpdateType(updateType);
			List<CollaborativeModule> cmList = findByExample(cm);
			if (cmList.size() == 1) {
				return cmList.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			log.error(
					"find CollaborativeModule instance by by id and updateType failed",
					re);
			throw re;
		}
	}

	// 已经存在记录返回空，数据库操作出错返回异常
	public CollaborativeModule createOneCollaborativeModuleWith7Params(
			String id, String businessType, String updateType,
			String processKey, String jpdlXml, String componentsXml,
			String originalJpdl) {
		log.debug("creating CollaborativeModule instance with 7Params: " + id
				+ "&" + updateType);
		try {
			CollaborativeModule cm = findBy2Prams(id, updateType);
			if (cm != null) {
				return null;

			} else {
				cm = new CollaborativeModule();
				cm.setId(id);
				cm.setBusinessType(businessType);
				cm.setUpdateType(updateType);
				cm.setProcessKey(processKey);
				cm.setJpdlXml(jpdlXml);
				cm.setComponentsXml(componentsXml);
				cm.setOriginalJpdl(originalJpdl);
				return (CollaborativeModule) save(cm);

			}

		} catch (RuntimeException re) {
			log.error(
					"create CollaborativeModule instance with 7Params failed",
					re);
			throw re;
		}

	}

	// 没有找到返回空，数据库操作出错返回异常
	public CollaborativeModule updateCollaborativeModuleBy2Params(String id,
			String updateType, String processKey, String jpdlXml,
			String componentsXml, String originalJpdl) {
		log.debug("updating CollaborativeModule instance by 2Params: " + id
				+ "&" + updateType);
		try {
			CollaborativeModule cm = findBy2Prams(id, updateType);
			if (cm != null) {
				cm.setProcessKey(processKey);
				cm.setJpdlXml(jpdlXml);
				cm.setComponentsXml(componentsXml);
				cm.setOriginalJpdl(originalJpdl);
				return (CollaborativeModule) saveOrUpdate(cm);
			}
			return null;
		} catch (RuntimeException re) {
			log.error("update CollaborativeModule instance by 2Params failed",
					re);
			throw re;
		}
	}

	// 没有找到返回DELETE_NOT_FINDED，数据库操作出错返回异常
	public String deleteCollaborativeModuleBy2Params(String id,
			String updateType) {
		log.debug("deleting CollaborativeModule instance by 2Params: " + id
				+ "&" + updateType);
		try {
			CollaborativeModule cm = findBy2Prams(id, updateType);
			if (cm != null) {
				delete(cm);
				return CollaborativeModuleConstant.DELETE_SUCCESS;
			} else {
				return CollaborativeModuleConstant.DELETE_NOT_FINDED;
			}
		} catch (RuntimeException re) {
			log.error("delete CollaborativeModule instance by 2Params failed",
					re);
			throw re;
		}
	}

	// 没有找到返回空，数据库操作出错返回异常
	public CollaborativeModule findByProcessKey(java.lang.String processKey) {
		log.debug("finding CollaborativeModule instance by processKey: "
				+ processKey);
		try {
			List cmList = findByProperty("processKey", processKey);
			if (cmList != null && cmList.size() > 0) {
				CollaborativeModule instance = (CollaborativeModule) cmList
						.get(0);
				return instance;
			} else
				return null;

		} catch (RuntimeException re) {
			log.error("find CollaborativeModule instance by processKey failed",
					re);
			throw re;
		}
	}

	// BUSINESS END

	// ////////////////////////////////////////////////////////////////////////////////////
	/*
	 * // 已经存在记录返回空，数据库操作出错返回异常 public CollaborativeModule
	 * createOneCollaborativeModuleWith7Params( String templateId, String
	 * businessType, String updateType, String processKey, String jpdlXml,
	 * String componentsXml, String originalJpdl) {
	 * log.debug("creating CollaborativeModule instance with 7Params: " +
	 * templateId); try { CollaborativeModule cm = findByTemplateId(templateId);
	 * if (cm != null) { return null;
	 * 
	 * } else { cm = new CollaborativeModule(); cm.setId(templateId);
	 * cm.setBusinessType(businessType); cm.setUpdateType(updateType);
	 * cm.setProcessKey(processKey); cm.setJpdlXml(jpdlXml);
	 * cm.setComponentsXml(componentsXml); cm.setOriginalJpdl(originalJpdl);
	 * return (CollaborativeModule) save(cm);
	 * 
	 * }
	 * 
	 * } catch (RuntimeException re) { log.error(
	 * "create CollaborativeModule instance with 7Params failed", re); throw re;
	 * }
	 * 
	 * }
	 * 
	 * // 没有找到返回空，数据库操作出错返回异常 public CollaborativeModule
	 * findByTemplateId(java.lang.String templateId) {
	 * log.debug("finding CollaborativeModule instance by templateId: " +
	 * templateId); try { List cmList = findByProperty("templateId",
	 * templateId); if (cmList != null && cmList.size() != 0) {
	 * CollaborativeModule instance = (CollaborativeModule) cmList .get(0);
	 * return instance; } else return null;
	 * 
	 * } catch (RuntimeException re) {
	 * log.error("find CollaborativeModule instance by templateId failed", re);
	 * throw re; } }
	 * 
	 * // 没有找到返回空，数据库操作出错返回异常 public CollaborativeModule
	 * updateCollaborativeModuleBy3Params( String templateId, String
	 * businessType, String updateType, String processKey, String jpdlXml,
	 * String componentsXml, String originalJpdl) {
	 * log.debug("updating CollaborativeModule instance by 3Params: " +
	 * processKey); try { CollaborativeModule cm = findByTemplateId(templateId);
	 * if (cm != null) { cm.setBusinessType(businessType);
	 * cm.setUpdateType(updateType); cm.setProcessKey(processKey);
	 * cm.setJpdlXml(jpdlXml); cm.setComponentsXml(componentsXml);
	 * cm.setOriginalJpdl(originalJpdl); return (CollaborativeModule)
	 * saveOrUpdate(cm); } return null; } catch (RuntimeException re) {
	 * log.error("update CollaborativeModule instance by 3Params failed", re);
	 * throw re; } }
	 * 
	 * // 没有找到返回空，数据库操作出错返回异常 public CollaborativeModule
	 * updateCollaborativeModuleByTemplateId( String templateId, String
	 * processKey, String jpdlXml, String componentsXml, String originalJpdl) {
	 * log.debug("updating CollaborativeModule instance by templateId: " +
	 * templateId); try { CollaborativeModule cm = findByTemplateId(templateId);
	 * if (cm != null) { cm.setProcessKey(processKey); cm.setJpdlXml(jpdlXml);
	 * cm.setComponentsXml(componentsXml); cm.setOriginalJpdl(originalJpdl);
	 * return (CollaborativeModule) saveOrUpdate(cm); } return null; } catch
	 * (RuntimeException re) { log.error(
	 * "update CollaborativeModule instance by templateId failed", re); throw
	 * re; }
	 * 
	 * }
	 */

}
